﻿Imports CRM.BusinessLayer
Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Partial Public Class frmAdminSLSEMailTemplates
    Private ListSLS As Integer
    Private Frm_StageStart As Integer
    Private Frm_StageEnd As Integer
    Public Property List_SLS() As Integer
        Get
            Return ListSLS
        End Get
        Set(ByVal Value As Integer)
            ListSLS = Value
        End Set
    End Property
    Private Sub frmAdminSLSEMailTemplates_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Try
            GeneralTasks.ChangeTheme(Me)
            GeneralTasks.EnumerateandFill(Me.LogicalChildren)
            Call SQL_TemplateList()
            Call SQL_AddNewList()
            Me.Btn_Remove.IsEnabled = False
            Me.Btn_SetCType.IsEnabled = False
            Me.Btn_SetStageEnd.IsEnabled = False
            Me.Btn_SetStageStart.IsEnabled = False
            Me.Frm_StageStart1.IsEnabled = False
            Me.Frm_StageEnd1.IsEnabled = False
            Dim strSQL As String = "select casestage_id,casestage_number,casestage_title from L_CaseStages"
            Dim caseStages As DataTable = CommonDataFunctions.GetSQLDatatable(strSQL)
            With Combo_StageStart
                .ItemsSource = CType(caseStages, IListSource).GetList
                .SelectedValuePath = "casestage_number"
                .DisplayMemberPath = "casestage_title"
            End With

            With Combo_StageEnd
                .ItemsSource = CType(caseStages, IListSource).GetList
                .SelectedValuePath = "casestage_number"
                .DisplayMemberPath = "casestage_title"
            End With

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminSLSEMailTemplates-frmAdminSLSEMailTemplates_Loaded")
        End Try
    End Sub

    Public Sub SQL_TemplateList()
        Try
            Dim StrSQL As String
            StrSQL = "SELECT I_SL_SLSMailTemplates.SLSMailTemplate_ID, D_EmailTemplates.EMailTemplate_Name, "
            StrSQL = StrSQL & " Case I_SL_SLSMailTemplates.SLSMailTemplate_CustomerType When 1 then 'Private' when 2 then 'Corporate' else 'All' end as CType, "
            StrSQL = StrSQL & " Case I_SL_SLSMailTemplates.SLSMailTemplate_StageEnable when 0 then 'New Lead' when 100 then 'Initiated' else L_CaseStages_Start.CaseStage_Title end AS CaseStage_Title_Start, "
            StrSQL = StrSQL & " Case I_SL_SLSMailTemplates.SLSMailTemplate_StageDisable when 99 then 'Sold' when 999 then 'Closed' else L_CaseStages_End.CaseStage_Title end AS CaseStage_Title_End"

            StrSQL = StrSQL & " FROM I_SL_SLSMailTemplates INNER JOIN"
            StrSQL = StrSQL & " D_EmailTemplates ON I_SL_SLSMailTemplates.SLSMailTemplate_TemplateID = D_EmailTemplates.EMailTemplate_ID LEFT OUTER JOIN"
            StrSQL = StrSQL & " L_CaseStages L_CaseStages_Start ON"
            StrSQL = StrSQL & " I_SL_SLSMailTemplates.SLSMailTemplate_StageEnable = L_CaseStages_Start.CaseStage_Number LEFT OUTER JOIN"
            StrSQL = StrSQL & " L_CaseStages L_CaseStages_End ON I_SL_SLSMailTemplates.SLSMailTemplate_StageDisable = L_CaseStages_End.CaseStage_Number"

            StrSQL = StrSQL & " WHERE I_SL_SLSMailTemplates.SLSMailTemplate_SLSID = " & Me.List_SLS

            StrSQL = StrSQL & " ORDER BY EMailTemplate_Name"
            Dim emailTemplatesList As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
            Me.List_Templates.ItemsSource = emailTemplatesList
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminSLSEMailTemplates-SQL_TemplateList")
        End Try
    End Sub
    Public Sub SQL_AddNewList()
        Try
            Dim StrSQL As String
            StrSQL = "Select EMailTemplate_ID, EMailTemplate_Name from D_EMailTemplates"
            StrSQL = StrSQL & " WHERE EMailTemplate_ID Not In ("
            StrSQL = StrSQL & "Select SLSMailTemplate_TemplateID From I_SL_SLSMailTemplates"
            StrSQL = StrSQL & " WHERE SLSMailTemplate_SLSID = " & Me.List_SLS
            StrSQL = StrSQL & ")"
            StrSQL = StrSQL & " ORDER BY EMailTemplate_Name"
            Dim emailTemplateList As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
            With Me.Combo_AddNew
                .ItemsSource = CType(emailTemplateList, IListSource).GetList
                .SelectedValuePath = "EMailTemplate_ID"
                .DisplayMemberPath = "EMailTemplate_Name"
            End With
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminSLSEMailTemplates-SQL_TemplateList")
        End Try
    End Sub


    Private Sub Btn_AddNew_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_AddNew.Click
        Try
            Dim StrSQL As String, StrMess As String, Rst As DataTable

            If Me.Combo_AddNew.SelectedIndex = -1 Then
                Call MsgBox("Please select a template to add", , "Error")
                Me.Combo_AddNew.Focus()
                Exit Sub
            End If

            StrSQL = "Insert Into I_SL_SLSMailTemplates (SLSMailTemplate_SLSID, SLSMailTemplate_TemplateID, SLSMailTemplate_StageDisable)"
            StrSQL = StrSQL & " VALUES(" & Me.List_SLS & ", " & Me.Combo_AddNew.SelectedValue & ",999)"
            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            StrMess = "EMail Template no " & Me.Combo_AddNew.SelectedValue & " (" & Me.Combo_AddNew.Text & ") added to SLS no. " & Me.List_SLS
            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (SL)", StrMess)

            'Me.List_Templates.Requery()
            Me.SQL_TemplateList()

            StrSQL = "Select SLSMailTemplate_ID from I_SL_SLSMailTemplates WHERE SLSMailTemplate_SLSID = " & Me.List_SLS & " AND SLSMailTemplate_TemplateID = " & Me.Combo_AddNew.SelectedValue
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                Rst.Dispose()
                Call MsgBox("New template details not found!", , "Error")
                Exit Sub
            End If

            Me.List_Templates = Rst.Rows(0)("SLSMailTemplate_ID")
            Rst.Dispose()

            'Me.Combo_AddNew.Requery()
            Me.SQL_AddNewList()
            ' Me.Combo_AddNew = Null

            Call List_Templates_AfterUpdate()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminSLSEMailTemplates-Btn_AddNew_Click")
        End Try
    End Sub
    Private Sub List_Templates_AfterUpdate()
        Try
            Dim StrSQL As String, Rst As DataTable
            Dim selectedTemplate As DataRow = Me.List_Templates.SelectedItem
            If selectedTemplate Is Nothing Then
                Me.Btn_Remove.IsEnabled = False
                Me.Btn_SetCType.IsEnabled = False
                Me.Frm_StageEnd1.IsEnabled = False
                Me.Frm_StageStart1.IsEnabled = False
                Me.Combo_StageEnd.IsEnabled = False
                Me.Combo_StageStart.IsEnabled = False
            Else
                Me.Btn_Remove.IsEnabled = True
                Me.Btn_SetCType.IsEnabled = True
                Me.Frm_StageEnd1.IsEnabled = True
                Me.Frm_StageStart1.IsEnabled = True
                Me.Combo_StageEnd.IsEnabled = False
                Me.Combo_StageStart.IsEnabled = False
                Me.Btn_SetStageStart.IsEnabled = True
                Me.Btn_SetStageEnd.IsEnabled = True
                StrSQL = "Select SLSMailTemplate_stageenable, SLSMailTemplate_stagedisable from I_SL_SLSMailTemplates Where SLSMailTemplate_ID = " & selectedTemplate(0)
                Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

                If Rst.Rows.Count = 0 Then
                    Rst.Dispose()
                    Call MsgBox("Template detals not found!", , "Error")
                    Exit Sub
                End If

                Select Case Rst.Rows(0)("SLSMailTemplate_Stageenable")

                    Case 0

                        Me.Option17.IsChecked = True
                        Me.Combo_StageStart.ItemsSource = Nothing
                        Me.Combo_StageStart.IsEnabled = False

                    Case 100
                        Me.Option19.IsChecked = True
                        Me.Combo_StageStart.ItemsSource = Nothing
                        Me.Combo_StageStart.IsEnabled = False


                    Case Else
                        Me.Option21.IsChecked = True
                        Me.Combo_StageStart = Rst.Rows(0)("SLSMailTemplate_Stageenable")
                        Me.Combo_StageStart.IsEnabled = True
                End Select

                Select Case Rst.Rows(0)("SLSMailTemplate_Stagedisable")

                    Case 99

                        Me.Option24.IsChecked = True
                        Me.Combo_StageEnd.ItemsSource = Nothing
                        Me.Combo_StageEnd.IsEnabled = False

                    Case 999
                        Me.Option26.IsChecked = True
                        Me.Combo_StageEnd.ItemsSource = Nothing
                        Me.Combo_StageEnd.IsEnabled = False


                    Case Else
                        Me.Option28.IsChecked = True
                        Me.Combo_StageEnd = Rst.Rows(0)("SLSMailTemplate_Stagedisable")
                        Me.Combo_StageEnd.IsEnabled = True
                End Select

                Rst.Dispose()

                Me.Frm_StageEnd1.IsEnabled = True
                Me.Frm_StageStart1.IsEnabled = True

                Me.Btn_Remove.IsEnabled = True
                Me.Btn_SetCType.IsEnabled = True
                Me.Btn_SetStageEnd.IsEnabled = False
                Me.Btn_SetStageStart.IsEnabled = False


            End If

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminSLSEMailTemplates-List_Templates_AfterUpdate")
        End Try
    End Sub

    Private Sub Option17_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option17.Checked
        Me.Frm_StageStart = 1
        Me.Combo_StageStart.IsEnabled = False
    End Sub

    Private Sub Option19_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option19.Checked
        Me.Frm_StageStart = 2
        Me.Combo_StageStart.IsEnabled = False
    End Sub

    Private Sub Option21_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option21.Checked
        Me.Frm_StageStart = 3
        Me.Combo_StageStart.IsEnabled = True
        Dim strSQL As String = "select casestage_id,casestage_number,casestage_title from L_CaseStages"
        Dim caseStages As DataTable = CommonDataFunctions.GetSQLDatatable(strSQL)
        With Combo_StageStart
            .ItemsSource = CType(caseStages, IListSource).GetList
            .SelectedValuePath = "casestage_number"
            .DisplayMemberPath = "casestage_title"
        End With
    End Sub

    Private Sub Option24_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option24.Checked
        Me.Frm_StageEnd = 1
        Me.Combo_StageEnd.IsEnabled = False
    End Sub

    Private Sub Option26_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option26.Checked
        Me.Frm_StageEnd = 2
        Me.Combo_StageEnd.IsEnabled = False
    End Sub

    Private Sub Option28_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option28.Checked
        Me.Frm_StageEnd = 3
        Me.Combo_StageEnd.IsEnabled = True
        Dim strSQL As String = "select casestage_id,casestage_number,casestage_title from L_CaseStages"
        Dim caseStages As DataTable = CommonDataFunctions.GetSQLDatatable(strSQL)
        With Combo_StageEnd
            .ItemsSource = CType(caseStages, IListSource).GetList
            .SelectedValuePath = "casestage_number"
            .DisplayMemberPath = "casestage_title"
        End With
    End Sub

    Private Sub Btn_Close_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Close.Click
        Me.Close()
    End Sub

    Private Sub Btn_Remove_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Remove.Click
        Try
            Dim StrSQL As String, StrMess As String
            Dim selectedTemplate As DataRow = Me.List_Templates.SelectedItem
            If selectedTemplate Is Nothing Then
                Call MsgBox("Please select a template to remove.", , "Error")
                Me.List_Templates.Focus()
                Exit Sub
            End If

            If MsgBox("Are you sure you want to remove this template?", vbYesNo, "Confirm delete") <> 6 Then Exit Sub

            StrSQL = "Delete from I_SL_SLSMailTemplates where SLSMailTemplate_ID = " & selectedTemplate(0)
            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            StrMess = "EMail template '" & selectedTemplate(1) & "' removed from SLS no. " & Me.List_SLS
            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (SL)", StrSQL)

            Me.SQL_TemplateList()
            'Me.List_Templates = Null
            Me.SQL_AddNewList()
            Me.List_Templates.Focus()
            Call List_Templates_AfterUpdate()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminSLSEMailTemplates-Btn_Remove_Click")
        End Try
    End Sub

    Private Sub Btn_SetCType_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_SetCType.Click
        Try

            Dim StrSQL As String, StrMess As String, IntCtype As Integer
            Dim selectedTemplate As DataRow = Me.List_Templates.SelectedItem
            If selectedTemplate Is Nothing Then
                Call MsgBox("Please select a template first", , "Error")
                Me.List_Templates.Focus()
                Exit Sub
            End If

            If MsgBox("Is this template for a specific client type?", vbYesNo, "Set Client Type") = 6 Then
                If MsgBox("Is this template only for corporate clients?", vbYesNo, "Set Client Type") = 6 Then
                    IntCtype = 2
                    StrMess = "Email Template '" & selectedTemplate(1) & "' set to corporate only"
                Else
                    IntCtype = 1
                    StrMess = "Email Template '" & selectedTemplate(1) & "' set to private only"
                End If
            Else
                IntCtype = 0
                StrMess = "Email Template '" & selectedTemplate(1) & "' set to all client types"
            End If

            StrSQL = "Update I_SL_SLSMailTemplates Set SLSMailTemplate_CustomerType = " & IntCtype & " WHERE SLSMailTemplate_ID = " & selectedTemplate(0)
            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (SL)", StrMess)

            Me.SQL_TemplateList()

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminSLSEMailTemplates-Btn_SetCType_Click")
        End Try
    End Sub

    Private Sub Btn_SetStageStart_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_SetStageStart.Click
        Try
            Dim selectedTemplate As DataRow = Me.List_Templates.SelectedItem
            Dim StrSQL As String, StrMess As String, IntStart As Integer
            If selectedTemplate Is Nothing Then
                MsgBox("Please select a template", , "Error")
                Exit Sub
            End If
            Select Case Me.Frm_StageStart

                Case 1
                    IntStart = 0
                    StrMess = " Mail Template '" & selectedTemplate(1) & "' in SLS no. " & Me.List_SLS
                    StrMess = StrMess & " set to be available from New Lead onwards."

                Case 2
                    IntStart = 100
                    StrMess = " Mail Template '" & selectedTemplate(1) & "' in SLS no. " & Me.List_SLS
                    StrMess = StrMess & " set to be available from Case Initiated onwards."

                Case Else
                    IntStart = Me.Combo_StageStart.SelectedValue
                    StrMess = " Mail Template '" & selectedTemplate(1) & "' in SLS no. " & Me.List_SLS
                    StrMess = StrMess & " set to be available from " & Me.Combo_StageStart.Text & " onwards."
            End Select

            StrSQL = "Update I_SL_SLSMailTemplates Set SLSMailTemplate_StageEnable = " & IntStart & " WHERE SLSMailTemplate_ID = " & selectedTemplate(0)
            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (SL)", StrMess)

            Me.SQL_TemplateList()
            Me.List_Templates.Focus()
            Call List_Templates_AfterUpdate()

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminSLSEMailTemplates-Btn_SetStageStart_Click")
        End Try
    End Sub

    Private Sub Btn_SetStageEnd_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_SetStageEnd.Click
        Try
            Dim selectedTemplate As DataRow = Me.List_Templates.SelectedItem
            Dim StrSQL As String, StrMess As String, IntEnd As Integer
            If selectedTemplate Is Nothing Then
                MsgBox("Please select a template", , "Error")
                Exit Sub
            End If
            Select Case Me.Frm_StageEnd

                Case 1
                    IntEnd = 99
                    StrMess = " Mail Template '" & selectedTemplate(1) & "' in SLS no. " & Me.List_SLS
                    StrMess = StrMess & " set to be available until Case Initiated."

                Case 2
                    IntEnd = 999
                    StrMess = " Mail Template '" & selectedTemplate(1) & "' in SLS no. " & Me.List_SLS
                    StrMess = StrMess & " set to be available until File Closed."

                Case Else
                    IntEnd = Me.Combo_StageEnd.SelectedValue
                    StrMess = " Mail Template '" & selectedTemplate(1) & "' in SLS no. " & Me.List_SLS
                    StrMess = StrMess & " set to be available until " & Me.Combo_StageEnd.Text
            End Select

            StrSQL = "Update I_SL_SLSMailTemplates Set SLSMailTemplate_Stagedisable = " & IntEnd & " WHERE SLSMailTemplate_ID = " & selectedTemplate(0)
            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (SL)", StrMess)

            Me.SQL_TemplateList()
            Me.List_Templates.Focus()
            Call List_Templates_AfterUpdate()

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminSLSEMailTemplates-Btn_SetStageEnd_Click")
        End Try
    End Sub

    Private Sub Combo_AddNew_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles Combo_AddNew.SelectionChanged

    End Sub

    Private Sub List_Templates_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles List_Templates.SelectionChanged
        Call List_Templates_AfterUpdate()
        Me.Btn_SetStageStart.IsEnabled = True
        Me.Btn_SetStageEnd.IsEnabled = True
    End Sub
End Class
